package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateShareProfitRuleDTO;
import com.itlong.cloud.POJO.PO.ShareProfitRuleInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateShareProfitRuleGetVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateShareProfitRuleDao;
import com.itlong.cloud.operate.service.IOperateShareProfitRuleService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      分润规则信息管理服务层接口实现类
 * </desc>
 *
 * @createDate 2019/05/20
 */
@Service
public class OperateShareProfitRuleServiceImpl implements IOperateShareProfitRuleService {

    @Autowired
    private IOperateShareProfitRuleDao iOperateShareProfitRuleDao;

    /**
     * <desc>
     *      分页获取分润规则信息
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 分润规则信息列表
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateShareProfitRuleGetVO> getPage(OperateShareProfitRuleDTO operateShareProfitRuleDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(operateShareProfitRuleDTO);
        Page<OperateShareProfitRuleGetVO> page = new Page<>();
        page.getPage().setPageSize(operateShareProfitRuleDTO.getPageSize());
        page.getPage().setCurrentPage(operateShareProfitRuleDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<OperateShareProfitRuleGetVO> operateShareProfitRuleGetVOS = iOperateShareProfitRuleDao.getPage(params);
        page.setRows(operateShareProfitRuleGetVOS,iOperateShareProfitRuleDao.getRecordCount(params));
        return page;
    }

    /**
     * <desc>
     *      添加分润规则信息
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(OperateShareProfitRuleDTO operateShareProfitRuleDTO) throws Exception {
        Date nowTime = new Date();
        ShareProfitRuleInfoPO shareProfitRuleInfoPO = new ShareProfitRuleInfoPO();
        BeanUtils.copyProperties(operateShareProfitRuleDTO,shareProfitRuleInfoPO);
        shareProfitRuleInfoPO.setRuleId(LogicIdUtil.bussinessId());
        shareProfitRuleInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
        shareProfitRuleInfoPO.setCreateUserId(operateShareProfitRuleDTO.getMyUserName());
        shareProfitRuleInfoPO.setOperateUserId(operateShareProfitRuleDTO.getMyUserName());
        shareProfitRuleInfoPO.setCreateTime(nowTime);
        shareProfitRuleInfoPO.setUpdateTime(nowTime);
        return iOperateShareProfitRuleDao.save(SqlUtil.durableData(shareProfitRuleInfoPO,PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      查询单个分润规则
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 分润规则
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateShareProfitRuleGetVO getOne(OperateShareProfitRuleDTO operateShareProfitRuleDTO) throws Exception {
        return iOperateShareProfitRuleDao.getOne(operateShareProfitRuleDTO.getRuleId());
    }

    /**
     * <desc>
     *      修改分润规则信息
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(OperateShareProfitRuleDTO operateShareProfitRuleDTO) throws Exception {
        Date nowTime = new Date();
        ShareProfitRuleInfoPO shareProfitRuleInfoPO = new ShareProfitRuleInfoPO();
        BeanUtils.copyProperties(operateShareProfitRuleDTO,shareProfitRuleInfoPO);
        shareProfitRuleInfoPO.setOperateUserId(operateShareProfitRuleDTO.getMyUserName());
        shareProfitRuleInfoPO.setUpdateTime(nowTime);
        return iOperateShareProfitRuleDao.update(SqlUtil.durableData(shareProfitRuleInfoPO,PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      删除分润规则信息，可批量
     * </desc>
     *
     * @param ruleIds 分润规则信息id，多个英文逗号相连
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer delete(String ruleIds) throws Exception {
        List<String> ruleIdList = StringHandlerUtil.splitStringList(ruleIds);
        return iOperateShareProfitRuleDao.delete(ruleIdList);
    }

    /**
     * <desc>
     *      校验分润规则是否闲置
     * </desc>
     *
     * @param ruleIds 分润规则信息id，多个英文逗号相连
     * @return true-是，false-否
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public boolean checkRuleIdle(String ruleIds) throws Exception {
        List<String> ruleIdList = StringHandlerUtil.splitStringList(ruleIds);
        int count = iOperateShareProfitRuleDao.getInUseCount(ruleIdList);
        return count == 0;
    }
}
